package com.zhida.jiyangcenter.membership

import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.resColor
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.base.utils.toast
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIMemberApps
import com.zhida.jiyangcenter.api.BalanceEntity
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.RechargeEntity
import com.zhida.jiyangcenter.mian.BaseFragment
import com.zhida.jiyangcenter.order.PayTypeEntity
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.fragment_recharge.*

@ContentLayout(R.layout.fragment_recharge)
class RechargeFragmnet : BaseFragment() {
    val adapter = Adapter()
    override fun initConfig(savedInstanceState: Bundle?) {
        rv_recharge.layoutManager = GridLayoutManager(context, 3)
        rv_recharge.adapter = adapter
        adapter.setNewData(ArrayList<Int>().apply {
            add(100)
            add(200)
            add(300)
            add(500)
            add(1000)
            add(5000)
        })
        adapter.setOnItemClickListener { _, view, position ->
            adapter.position = position
            adapter.notifyDataSetChanged()
        }
        tv_account.text = "当前账户：${SP.account.get().toString()}"
        tv_more.setOnClickListener {
            startActivity<PayMoreRechargeActivity> {}
        }
        tv_pay_order.setOnClickListener {
            if (adapter.position != -1) {
                lifecycleScope.request({
                    APIMemberApps.service().recharg(adapter.data[adapter.position])
                }, object : BaseAPI<RechargeEntity>() {
                    override fun onSuccess(t: RechargeEntity?, state: Int, msg: String) {
                        startActivity<PayRechargeActivity> {
                            putExtra("account", t?.account)
                            putExtra("balance", t?.balance)
                            putExtra("order_code", t?.order_code)
                            putExtra("pay_money", adapter.data[adapter.position])
                        }
                    }

                    override fun onError(e: APIException) {
                        tipFail(e.eMessage)
                    }

                })
            } else {
                toast("请选择充值金额")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.request(
            { APIMemberApps.service().balance() },
            object : BaseAPI<BalanceEntity>() {
                override fun onSuccess(t: BalanceEntity?, state: Int, msg: String) {
                    tv_money.text = "￥${t?.balance ?: "0.00"}"
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }

    class Adapter : BaseQuickAdapter<Int, BaseViewHolder>(R.layout.item_recharge) {
        var position: Int = -1
        override fun convert(helper: BaseViewHolder, item: Int?) {
            helper.setText(R.id.tv_more, "${item}元")
            if (position == helper.adapterPosition) {
                helper.getView<TextView>(R.id.tv_more).apply {
                    setTextColor(mContext.resColor(R.color.white))
                    setBackgroundResource(R.drawable.bg_home_fragment_fill_blue)
                }
            } else {
                helper.getView<TextView>(R.id.tv_more).apply {
                    setTextColor(mContext.resColor(R.color.textColor))
                    setBackgroundResource(R.drawable.bg_empty_gray8)
                }
            }
        }

    }
}